//
//  MZExtensionUIColor.swift
//  MZTool
//
//  Created by gby on 2022/3/7.
//

import UIKit


extension MZKit where Base == UIColor {
    
    public func animationColorChange(toColor: UIColor?, pro: CGFloat) -> UIColor{
        guard toColor != nil else {
            return base
        }
        let components = base.cgColor.components
        let r = components?[0] ?? 0.0
        let g = components?[1] ?? 0.0
        let b = components?[2] ?? 0.0
        let a = components?[3] ?? 0.0
        
        let tcomponents = toColor?.cgColor.components
        let tr = tcomponents?[0] ?? 0.0
        let tg = tcomponents?[1] ?? 0.0
        let tb = tcomponents?[2] ?? 0.0
        let ta = tcomponents?[3] ?? 0.0
        
        let progress = CGFloat(Int(min(abs(pro), 1.0) * 100))/100.00
        let R = r + progress * (tr - r)
        let G = g + progress * (tg - g)
        let B = b + progress * (tb - b)
        let A = a + progress * (ta - a)
        
        return UIColor(red: R, green: G, blue: B, alpha: A)
    }
    
}
